package com.websuites.web.servlet;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.websuites.core.request.WsHttpServletRequest;
import com.websuites.core.request.XMLBex;
import com.websuites.core.request.XMLBexs;
import com.websuites.core.response.WsHttpServletResponse;

@SuppressWarnings("unchecked")
public abstract class WsHttpServlet extends HttpServlet 
{
	private static final long serialVersionUID = 3145974737130622521L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException
	{		
		processRequest((WsHttpServletRequest)request, (WsHttpServletResponse)response, "GET");
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException
	{
		processRequest((WsHttpServletRequest)request,(WsHttpServletResponse) response, "POST");
	}

	protected abstract void processRequest(WsHttpServletRequest request, WsHttpServletResponse response, String method)
		throws ServletException, IOException;

	protected XMLBexs createBex(HttpServletRequest req, HttpServletResponse resp, String method)
	{
		String bexname = req.getParameter("bex");
		if(bexname==null)
		{
			return null;
		}
		XMLBexs xmlBexs = new XMLBexs();
		XMLBex 	xmlBex=	new XMLBex();
		xmlBex.putBex(bexname);
		xmlBexs.addBex(xmlBex);
		
		Enumeration enu = req.getParameterNames();
		while (enu.hasMoreElements())
		{
			String name = (String)enu.nextElement();
			String value = req.getParameter(name);
			xmlBex.addParam(name, value);
		}
		return xmlBexs;
	}
}
